
package tourmsystem.querry.controller;

import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import tourmsystem.querry.view.SearchByName;
import tourmsystem.querry.view.seachAll;
import tourmsystem.querry.view.searchById;
// lop dieu khien truy van
public class QueryController{
    private static Connection conn;
    private static String username =  "root";
    private  static String pass ="";
    private static String driver = "com.mysql.jdbc.Driver";
    private Statement stt;

    private static String url = "jdbc:mysql://localhost/qlt" ;

    //private static String url = "jdbc:mysql://localhost:3306/tourmanagement" ;

    public String name;
    private ResultSet rs;
    private boolean check=false;
  
    public void Connect()
    {
         try
        {
            Class.forName(driver);
            conn = DriverManager.getConnection(url,username,pass);
            System.out.println("Connect succesful");

        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    //phuong thuc dong ket noi du lieu 
    public  void close()
    {
        if(conn!=null)
        {
            try
            {
                conn.close();
            }
            catch(SQLException e)
            {
                System.out.println(e.getMessage());
            }
        }
    }
   public ResultSet read(String sqlQuery)
    {
        try
        {
            Connect();  
            stt = conn.createStatement();
            rs = stt.executeQuery(sqlQuery);
        }
        catch(SQLException e)
        {
            System.out.println(e.getMessage());
        }
                    return rs;
    }
   // Cau lenh tim kiem theo id
      public boolean search(String r,String tablename) throws SQLException{
          try
        {
            Connect();  
            System.out.println("Seach");
            searchById s= new searchById();
            String sqlQuery="select * from " +tablename+ " where id ='"+r+"'";
            stt = conn.createStatement();
            ResultSet rs = stt.executeQuery(sqlQuery);
            while(rs.next()){
               check=true;
            }
        }
        catch(SQLException e)
        {
            System.out.println(e.getMessage());
        }
	return check;
        
   }
      //Cau lenh tim kiem  theo tu khoa
        public boolean search2(String r,String tablename) throws SQLException{
          try
        {
            Connect();  
            System.out.println("Seach");
            SearchByName s2= new SearchByName();
            String sqlQuery="select * from " +tablename+ " where id ='"+r+"' OR Name = '"+r+"' OR Phone = '"+r+"' OR Email = '"+r+"' OR Address = '"+r+"' OR accountNum = '"+r+"' OR quantity = '"+r+"'";
            //String sqlQuery ="select * from "+tablename+ " where Name ='"+r"'";
               // String sqlQuery="select * from " +tablename+ " where Name ='"+r+"'";
       
            stt = conn.createStatement();
            ResultSet rs = stt.executeQuery(sqlQuery);
            while(rs.next()){
               check=true;
            }
            System.out.println("Testt " + rs.getRow());
        }
        catch(SQLException e)
        {
            System.out.println(e.getMessage());
        }
	return check;
        
   }
         public boolean search3(String r,String tablename) throws SQLException{
          try
        {
            Connect();  
            System.out.println("Seach");
            SearchByName s3= new SearchByName();
            String sqlQuery="select * from " +tablename+ " where id ='"+r+"' OR name = '"+r+"' OR startDate = '"+r+"' OR duration = '"+r+"' OR type = '"+r+"'  OR price = '"+r+"' OR discount = '"+r+"' OR schedule = '"+r+"' OR maxP= '"+r+"' OR imgLink = '"+r+"'";
           
       
            stt = conn.createStatement();
            ResultSet rs = stt.executeQuery(sqlQuery);
            while(rs.next()){
               check=true;
            }
            System.out.println("Testt " + rs.getRow());
        }
        catch(SQLException e)
        {
            System.out.println(e.getMessage());
        }
	return check;
        
   }
       
   // cau lenh tim kiem tat ca  
   public boolean search1(String tablename) throws SQLException{
          try
        {
            Connect();  
            System.out.println("Seach");
            seachAll s1= new seachAll();
            String sqlQuery="select * from " +tablename ;
            stt = conn.createStatement();
            ResultSet rs = stt.executeQuery(sqlQuery);
            while(rs.next()){
               check=true;
           }
        }
        catch(SQLException e)
        {
            System.out.println(e.getMessage());
        }
	return check;
        
   }
     // cau lenh tim kiem theo tu khoa 
   public static  void main(String args[]) throws Exception
      {
          QueryController q= new QueryController();
          q.Connect();
      }
   
}
